package com.yiren.manager.service.impl.elder;

import java.util.List;

import javax.transaction.Transactional;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.yiren.core.exception.MyException;
import com.yiren.core.service.GenericServiceImpl;
import com.yiren.core.utils.FavoriteSet;
import com.yiren.core.utils.Favorites;
import com.yiren.entity.YrBed;
import com.yiren.entity.YrElder;
import com.yiren.entity.YrElderArrearage;
import com.yiren.entity.YrElderBeHospital;
import com.yiren.entity.YrElderBedRecord;
import com.yiren.entity.YrElderPayRecord;
import com.yiren.entity.YrElderWallet;
import com.yiren.entity.YrMealCard;
import com.yiren.entity.YrUserVipCard;
import com.yiren.entity.YrUserVipCardRecord;
import com.yiren.manager.repository.beds.YrBedRepository;
import com.yiren.manager.repository.cost.YrElderArrearageRepository;
import com.yiren.manager.repository.cost.YrElderPayRecordRepository;
import com.yiren.manager.repository.elder.YrElderBeHospitalRepository;
import com.yiren.manager.repository.elder.YrElderBedRecordRepository;
import com.yiren.manager.repository.elder.YrElderRepository;
import com.yiren.manager.repository.elder.YrElderWalletRepository;
import com.yiren.manager.repository.messhall.YrMealCardRepositoty;
import com.yiren.manager.repository.vip.YrUserVipCardRecordRepository;
import com.yiren.manager.repository.vip.YrUserVipCardRepository;
import com.yiren.manager.service.iface.elder.YrElderBeHospitalService;

@Service
public class YrElderBeHospitalServiceImpl extends GenericServiceImpl<YrElderBeHospital, String> implements YrElderBeHospitalService{

	@Autowired
	private YrBedRepository bedRepository;
	
	@Autowired
	private YrUserVipCardRepository userVipCardRepository;
	
	@Autowired
	private YrElderRepository elderRepository;
	
	@Autowired
	private YrElderBedRecordRepository elderBedRecordRepository;
	
	@Autowired
	private YrElderWalletRepository elderWalletRepository;
	
//	@Autowired
//	private YrBedCostRecordRepository bedCostRecordRepository;
	
	@Autowired
	private YrUserVipCardRecordRepository userVipCardRecordRepository;
	
//	@Autowired
//	private YrBaseServiceCostRecordRepository baseServiceCostRecordRepository;
//	
//	@Autowired
//	private YrNursingCostRecordRepository nursingCostRecordRepository;
	
	@Autowired
	private YrElderPayRecordRepository elderPayRecordRepository;
	
	@Autowired
	private YrMealCardRepositoty mealCardRepository;
	
	@Autowired
	private YrElderArrearageRepository elderArrearageRepository;
	
	@Autowired
	public YrElderBeHospitalServiceImpl(YrElderBeHospitalRepository repository) {
		super(repository);
	}

	@Transactional
	public void addByFavoriteSet(FavoriteSet entitys) throws MyException {

		YrElderBeHospital elderBeHospital = entitys.getFavorite("elderBeHospital",YrElderBeHospital.class);
		
		YrElderBedRecord elderBedRecord = entitys.getFavorite("bedRecord",YrElderBedRecord.class);
		List<YrBed> beds = entitys.getFavorite("bedList",List.class);
		YrUserVipCard userVipCard = entitys.getFavorite("userVipCard",YrUserVipCard.class);
		YrElder elder = entitys.getFavorite("elder",YrElder.class);
		
		YrElderWallet elderWallet = entitys.getFavorite("elderWallet",YrElderWallet.class);
		YrUserVipCardRecord userVipCardRecord = entitys.getFavorite("userVipCardRecord",YrUserVipCardRecord.class);
		
//		List<YrElderPayRecord> elderPayRecords = entitys.getFavorite("elderPayRecordList", List.class);
		YrMealCard mealCard = entitys.getFavorite("mealCard",YrMealCard.class);
		
		if(null!=elderWallet){
			elderWalletRepository.save(elderWallet);
		}
		if(null!=userVipCardRecord){
			userVipCardRecordRepository.save(userVipCardRecord);
		}
		
		if(userVipCard!=null){
			userVipCardRepository.save(userVipCard);
		}
		this.repository.save(elderBeHospital);
		for(YrBed bed : beds){
			bedRepository.save(bed);	
		}
		elderRepository.save(elder);
		if(null!=elderBedRecord){
			elderBedRecordRepository.save(elderBedRecord);	
		}
//		for(YrElderPayRecord elderPayRecord : elderPayRecords){
//			elderPayRecordRepository.save(elderPayRecord);
//		}
		if(null!=mealCard){
			mealCardRepository.save(mealCard);
		}
	}

	public void modifyByFavorites(Favorites entitys) throws MyException {
		// TODO Auto-generated method stub
		
	}

	@Transactional
	public void executeTurnRoomFavorites(FavoriteSet entitys) throws MyException {
		YrElderBeHospital elderBeHospital = entitys.getFavorite("elderBeHospital",YrElderBeHospital.class);
		List<YrBed> bedList = entitys.getFavorite("bedList", List.class);
		YrElderBedRecord elderBedRecord = entitys.getFavorite("bedRecord", YrElderBedRecord.class);
		List<YrElderPayRecord> elderPayRecords = entitys.getFavorite("elderPayRecords", List.class);
		List<YrElderArrearage> elderArrearages = entitys.getFavorite("elderPayRecords", List.class);
		YrElderWallet elderWallet = entitys.getFavorite("elderWallet", YrElderWallet.class);
		
		this.repository.save(elderBeHospital);
		for(YrBed bed : bedList){
			bedRepository.save(bed);
		}
		if(null!=elderBedRecord){
			elderBedRecordRepository.save(elderBedRecord);
		}
		for(YrElderPayRecord elderPayRecord : elderPayRecords){
			elderPayRecordRepository.save(elderPayRecord);
		}
		for(YrElderArrearage elderArrearage : elderArrearages){
			elderArrearageRepository.save(elderArrearage);
		}
		if(null!=elderWallet){
			elderWalletRepository.save(elderWallet);
		}
		
	}
	
}
